package cn.telling.common.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * 
 * @ClassName: ExcelUtils
 * @Description: Excel瀵煎叆鐨勫伐鍏风被
 * @author 闄堣禑
 * @date 2015骞�6鏈�12鏃� 涓婂崍10:47:57
 * 
 */
public class ExcelUtils {

	private static Logger LOGGER = LoggerFactory.getLogger(ExcelUtils.class);
    // 鎴愬姛
    public static final Integer STATUS_OK = Integer.valueOf(1);
    // 澶辫触
    public static final Integer STATUS_NO = Integer.valueOf(0);

    /**
     * 绉佹湁鍖栨瀯閫犲櫒
     */
    private ExcelUtils() {

    }

    /**
     * 鑾峰彇excel鏂囦欢涓殑鏁版嵁瀵硅薄
     * 
     * @param is excel鏂囦欢璇诲叆娴�
     * @param excelColumnNames excel涓瘡涓瓧娈电殑鑻辨枃鍚�(搴旇涓巔ojo瀵硅薄鐨勫瓧娈靛悕涓�鑷�,椤哄簭涓巈xcel涓�鑷�)
     * @return excel姣忚鏄痩ist涓�鏉¤褰曪紝map鏄搴旂殑"瀛楁鍚�-->鍊�"
     * @throws Exception
     */
    public static List<Map<String, String>> getImportData(InputStream is, List<String> excelColumnNames)
            throws Exception {
        LOGGER.debug("InputStream:{}", is);
        if (is == null) {
            return Collections.emptyList();
        }

        Workbook workbook = null;
        try {
            // 鎷垮埌excel
            workbook = Workbook.getWorkbook(is);
        } catch (BiffException e) {
            LOGGER.error(e.getMessage(), e);
            return Collections.emptyList();
        } catch (IOException e) {
            LOGGER.error(e.getMessage(), e);
            return Collections.emptyList();
        }
        LOGGER.debug("workbook:{}", workbook);

        if (workbook == null) {
            return Collections.emptyList();
        }

        // 绗竴涓猻heet
        Sheet sheet = workbook.getSheet(0);
        // 琛屾暟
        int rowCounts = sheet.getRows() - 1;
        LOGGER.debug("rowCounts:{}", rowCounts);
        List<Map<String, String>> list = new ArrayList<Map<String, String>>(rowCounts);

        // 鍙岄噸for寰幆鍙栧嚭鏁版嵁
        for (int i = 1; i < rowCounts + 1; i++) {
            Map<String, String> params = new HashMap<String, String>();
            // i,j i:琛� j:鍒�
            for (int j = 0; j < excelColumnNames.size(); j++) {
                Cell cell = sheet.getCell(j, i);
                params.put(excelColumnNames.get(j), cell.getContents().trim());
            }

            list.add(params);
        }

        return list;
    }

    /**
     * 鑾峰彇瀵煎叆鏁版嵁涓哄璞＄殑List
     * 
     * @param data
     * @param clazz
     * @param excelColumnNames
     * @param checkExcel
     * @param <T>
     * @return
     * @throws Exception
     */
    public static <T> List<T> makeData(List<Map<String, String>> data, Class<T> clazz, List<String> excelColumnNames,
            CheckExcel checkExcel) throws Exception {
        if (data == null || data.isEmpty() || clazz == null || checkExcel == null) {
            return Collections.emptyList();
        }

        List<T> result = new ArrayList<T>(data.size());
        for (Map<String, String> d : data) {
            if (checkExcel != null && !checkExcel.check(d)) {
                continue;
            }
            T entity = clazz.newInstance();
            for (String column : excelColumnNames) {
                BeanUtils.setProperty(entity, column, d.get(column));
            }

            result.add(entity);
        }

        return result;
    }

}
